package com.chenchao.javafile;

import java.io.*;
import java.util.Date;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/19 15:53
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.javafile.Test14.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * 对象流
 */
public class Test14 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        test01();
        test02();
    }

    /**
     * 利用字节数组流测试对象流
     *
     * @throws IOException            IO异常
     * @throws ClassNotFoundException 类异常
     */
    public static void test01() throws IOException, ClassNotFoundException {
        byte[] b;
        //存储对象
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                new BufferedOutputStream(byteArrayOutputStream));
        //只能写入序列化对象
        objectOutputStream.writeObject(new Date());
        objectOutputStream.writeObject("go die,java");
        objectOutputStream.writeObject(new Cat());
        //先关闭对象流
        objectOutputStream.close();
        b = byteArrayOutputStream.toByteArray();

        //读出对象
        ObjectInputStream objectInputStream = new ObjectInputStream(
                new BufferedInputStream(new ByteArrayInputStream(b)));
        //按照写的顺序进行反序列化操作
        Object o = objectInputStream.readObject();
        Object o1 = objectInputStream.readObject();
        Object o2 = objectInputStream.readObject();
        //根据是否多态选择是否强制转换
        if (o instanceof Date) {
            System.out.println(o);
        }
        if (o1 instanceof String) {
            System.out.println((String) o1);
        }
        if (o2 instanceof Cat) {
            System.out.println(o2);
        }
    }

    /**
     * 文件测试对象流
     */
    public static void test02() {
        //写入序列化
        try (ObjectOutputStream objectOutputStream = new ObjectOutputStream
                (new BufferedOutputStream(new FileOutputStream("myjava01/src/fo/obj.txt")))) {
            objectOutputStream.writeObject(new Date());
            objectOutputStream.writeObject("shift java");
            objectOutputStream.writeObject(new Cat());
        } catch (IOException e) {
            e.printStackTrace();
        }
        //读出反序列化
        try(ObjectInputStream objectInputStream = new ObjectInputStream(new BufferedInputStream
                (new FileInputStream("myjava01/src/fo/obj.txt")))) {
            Object o1 = objectInputStream.readObject();
            Object o2 = objectInputStream.readObject();
            Object o3 = objectInputStream.readObject();
            System.out.println(o1);
            System.out.println(o2);
            System.out.println(o3);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

/**
 * 可序列化对象
 */
class Cat implements Serializable {
    String name = "小黄";
    int age = 5;
    transient String host = "伏地魔";
    //不想被序列化的内容，用于隐藏机密

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", host='" + host + '\'' +
                '}';
    }
}
